package com.renne.ai.dbcmcp.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.renne.ai.dbcmcp.datasource.DynamicDataSource;
import com.renne.ai.dbcmcp.datasource.DynamicDataSourceUtil;
import com.renne.ai.dbcmcp.domain.SysDataSource;
import com.renne.ai.dbcmcp.properties.AppProperties;
import com.renne.ai.dbcmcp.utils.DataSourceComposeUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;

/**
 * Druid 配置多数据源
 *
 * @author LiuYu
 * @since 2025-03-31 18:48
 */
@Configuration
public class DruidConfig {

    @Resource
    private AppProperties appProperties;

    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dataSource() {
        // 将 AppProperties 中的连接信息转换为 SysDataSource
        SysDataSource sysDataSource = new SysDataSource();
        sysDataSource.setName(appProperties.getDbName());
        sysDataSource.setDbType(appProperties.getDbType());
        sysDataSource.setHost(parseHostFromUrl(appProperties.getDbUrl()));
        sysDataSource.setPort(parsePortFromUrl(appProperties.getDbUrl(), appProperties.getDbType())); // 从URL中解析端口号
        sysDataSource.setUsername(appProperties.getDbUser());
        sysDataSource.setPassword(appProperties.getDbPassword());
        if (appProperties.getSchemaName() != null) {
            sysDataSource.setSchemaName(appProperties.getSchemaName());
        }

        // 使用 DataSourceComposeUtils 创建 DruidDataSource
        DruidDataSource druidDataSource = DataSourceComposeUtils.composeDruidDataSource(sysDataSource);

        // 将主数据源添加到动态数据源工具中
        DynamicDataSourceUtil.addTargetDataSource("default", druidDataSource);

        // 创建动态数据源并返回
        return new DynamicDataSource(druidDataSource, DynamicDataSourceUtil.dataSourceMap);
    }

    private Integer parsePortFromUrl(String jdbcUrl, String dbType) {
        // 定义不同数据库的默认端口
        int defaultPort = 3306; // MySQL 默认端口
        if (dbType.startsWith("postgresql")) {
            defaultPort = 5432; // PostgreSQL 默认端口
        } else if (dbType.startsWith("sqlserver")) {
            defaultPort = 1433; // SQL Server 默认端口
        }

        // 正则表达式匹配端口号
        String regex = "\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b:\\d+";
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
        java.util.regex.Matcher matcher = pattern.matcher(jdbcUrl);

        if (matcher.matches()) {
            String[] parts = jdbcUrl.split(":");
            // 提取端口号
            return Integer.parseInt(parts[1]);
        } else {
            // 如果未匹配到端口号，返回默认端口
            return defaultPort;
        }
    }

    private String parseHostFromUrl(String jdbcUrl) {
        // 定义不同数据库的默认主机
        String defaultHost = "localhost"; // 默认主机地址

        // 正则表达式匹配主机地址
        String regex = "\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b:\\d+";
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
        java.util.regex.Matcher matcher = pattern.matcher(jdbcUrl);

        if (matcher.matches()) {
            String[] parts = jdbcUrl.split(":");
            // 提取主机地址
            return parts[0];
        } else {
            // 如果未匹配到主机地址，返回默认主机
            return defaultHost;
        }
    }

}

